import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request } from '@nestjs/common';
import { CommentService } from './comment.service';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { Role } from '../auth/enums/role.enum';
import { Comment } from './entities/comment.entity';

@ApiTags('评论')
@Controller('comments')
export class CommentController {
  constructor(private readonly commentService: CommentService) {}

  @Post()
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '创建评论' })
  @ApiResponse({ status: 201, description: '创建成功', type: Comment })
  @ApiResponse({ status: 400, description: '参数错误' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 404, description: '图书或用户不存在' })
  create(@Body() createCommentDto: CreateCommentDto, @Request() req) {
    return this.commentService.create(createCommentDto, req.user.id);
  }

  @Get()
  @ApiOperation({ summary: '获取所有评论' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Comment] })
  findAll() {
    return this.commentService.findAll();
  }

  @Get('book/:bookId')
  @ApiOperation({ summary: '获取指定图书的所有评论' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Comment] })
  @ApiResponse({ status: 404, description: '图书不存在' })
  findAllByBook(@Param('bookId') bookId: string) {
    return this.commentService.findAllByBook(bookId);
  }

  @Get('user/:userId')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '获取指定用户的所有评论' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Comment] })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 404, description: '用户不存在' })
  findAllByUser(@Param('userId') userId: string, @Request() req) {
    // 只能查看自己的评论，除非是管理员
    const targetUserId = req.user.roles?.includes(Role.ADMIN) ? userId : req.user.id;
    return this.commentService.findAllByUser(targetUserId);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取指定评论' })
  @ApiResponse({ status: 200, description: '获取成功', type: Comment })
  @ApiResponse({ status: 404, description: '评论不存在' })
  findOne(@Param('id') id: string) {
    return this.commentService.findOne(id);
  }

  @Patch(':id')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '更新评论' })
  @ApiResponse({ status: 200, description: '更新成功', type: Comment })
  @ApiResponse({ status: 400, description: '参数错误' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 404, description: '评论不存在' })
  update(
    @Param('id') id: string,
    @Body() updateCommentDto: UpdateCommentDto,
    @Request() req,
  ) {
    const isAdmin = req.user.roles?.includes(Role.ADMIN);
    return this.commentService.update(id, updateCommentDto, req.user.id, isAdmin);
  }

  @Delete(':id')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '删除评论' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 404, description: '评论不存在' })
  remove(@Param('id') id: string, @Request() req) {
    const isAdmin = req.user.roles?.includes(Role.ADMIN);
    return this.commentService.remove(id, req.user.id, isAdmin);
  }

  @Post('sample')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: '添加示例评论数据' })
  @ApiResponse({ status: 201, description: '添加成功', type: [Comment] })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  addSampleComments() {
    return this.commentService.addSampleComments();
  }
}
